/*
 This file will contain all the functions that have
 to do with login and establishing connections to the ftp server
 */

#include "cli_net.h"


/*
 This function will log into the server
 It is assumed the cmd_socket is ready for readying
 @param hostname: the hostname of the server
 @param port: the port the server is listening on
 */
bool logInToServer(void)
{
    char username[128], *password;
    
    char message[256];
    
    
    //get the inital response of the server
    RESP *r = sendAndReceive(NULL, C_RDY);
    
    //if the server is not ready for login, print the response and exit
    if(!r) return false;

    
    freeResp(&r);
    
    
    printf("User Name: ");
    scanf("%127s", username);
    
    
    sprintf(message, "USER %s\r\n", username);
    
    r = sendAndReceive(message, C_GOODUSR);
    
    if(!r) return false;
    
    
    password = getpass("Password:");
    
    //if there is no password, replace it with a space
    if(!strcmp(password, ""))
       strcpy(password, " ");
   
       
    
    sprintf(message, "PASS %s\r\n", password);
    
    //zero out password in memory
    bzero(password, _SC_PASS_MAX);
    
    r = sendAndReceive(message, C_LOGGEDIN);
    

    
    if(!r) return false;
    
    freeResp(&r);
    
    
    return true;
    
    
    
    
    
    
    
    
}

